﻿using BCGM.MedicalWisdom.IServices;
using BCGM.MedicalWisdom.Model;
using BCGM.MedicalWisdom.Model.BCGM_PatientModel;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace BCGM.MedicalWisdom.Api.Controllers
{
    /// <summary>
    /// 后台患者管理——收货地址
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    //[Authorize(Permissions.Name)]
    public class addressController : ControllerBase
    {
        /// <summary>
        /// 服务器接口，因为是模板生成，所以首字母是大写的，自己可以重构下
        /// </summary>
        private readonly IaddressServices _addressServices;
        private readonly IDepartmentServices _departmentServices;

        public addressController(IaddressServices addressServices, IDepartmentServices departmentServices)
        {
            _addressServices = addressServices;
            _departmentServices = departmentServices;
        }
        /// <summary>
        /// 收货地址显示列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="key"></param>
        /// <param name="intPageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<PageModel<address>>> Get(int page = 1, string key = "", int intPageSize = 50)
        {
            if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key))
            {
                key = "";
            }

            Expression<Func<address, bool>> whereExpression = a => true;

            return new MessageModel<PageModel<address>>()
            {
                msg = "获取成功",
                success = true,
                response = await _addressServices.QueryPage(whereExpression, page, intPageSize)
            };

        }
        /// <summary>
        /// 根据患者Id查询收货地址列表
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<List<address>>> GetAddressById(int pid)
        {
            if (!pid.IsNotEmptyOrNull())
            {
                return new MessageModel<List<address>>()
                {
                    msg = "获取失败，请传入Id",
                    success = false
                };
            }

            return new MessageModel<List<address>>()
            {
                msg = "获取成功",
                success = true,
                response = await _addressServices.GetAddressesById(pid)
            };

        }
        /// <summary>
        /// 根据Id查询收货地址
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<MessageModel<address>> Get(string id)
        {
            return new MessageModel<address>()
            {
                msg = "获取成功",
                success = true,
                response = await _addressServices.QueryById(id)
            };
        }
        /// <summary>
        /// 添加收货地址
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<MessageModel<string>> Post([FromBody] address request)
        {
            var data = new MessageModel<string>();

            var id = await _addressServices.Add(request);
            data.success = id > 0;
            if (data.success)
            {
                data.response = id.ObjToString();
                data.msg = "添加成功";
            }

            return data;
        }
        /// <summary>
        /// 修改收货地址
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<MessageModel<string>> Put([FromBody] address request)
        {
            var data = new MessageModel<string>();
            data.success = await _addressServices.Update(request);
            if (data.success)
            {
                data.msg = "更新成功";
                data.response = request?.AddId.ObjToString();
            }

            return data;
        }
        /// <summary>
        /// 删除收货地址
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<MessageModel<string>> Delete(int id)
        {
            var data = new MessageModel<string>();
            var model = await _addressServices.QueryById(id);
            model.IsDeleted = true;
            data.success = await _departmentServices.Update(model);
            if (data.success)
            {
                data.msg = "删除成功";
                data.response = model?.AddId.ObjToString();
            }

            return data;
        }
    }
}